﻿@using Newtonsoft.Json;
@using OnWork.DataCenter.Utils.Caches;
@using System.Collections.Concurrent;
@page "/DataSource/Variables"
<PageTitle>临时变量</PageTitle>
@inherits ComponentBaseDataCenter

<div>
    <h3>临时变量</h3>

    <Table TItem="VariablesVO" UseInjectDataService="false" IsPagination="false" ShowLineNo="true"
           IsStriped="true" IsBordered="true" ShowSkeleton="true" IsMultipleSelect="true" ShowEmpty="true" ShowLoading="true" ShowColumnList="true"
           ShowToolbar="true" ShowExtendButtons="true"
           ShowSearch="true" ShowAdvancedSearch="false"
           AutoGenerateColumns="true" EditMode="EditMode.Popup"
           OnQueryAsync="@QueryAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="OnDeleteAsync" ShowToastAfterSaveOrDeleteModel="false">
        @* <RowButtonTemplate>
        <TableCellButton Color="Color.Info" Icon="fa-solid fa-pen" Text="流程测试" OnClick="@(()=>OnTestAsync(context))" IsAsync="true" />
        <TableCellPopconfirmButton Color="Color.Info" Icon="fa-solid fa-pen" Text="测试" />
        </RowButtonTemplate>*@
        @* <EditTemplate>
        <TaskConfigEditView Config="@context" />
        </EditTemplate>*@
    </Table>
</div>
@code {

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="options"></param>
    /// <returns></returns>
    private async Task<QueryData<VariablesVO>> QueryAsync(QueryPageOptions options)
    {
        var result = new QueryData<VariablesVO>()
            {
                Items = VariablesCache.GETDict().Select(x => new VariablesVO { Key = x.Key, Value = x.Value })
            };

        return await Task.FromResult(result);
    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnDeleteAsync(IEnumerable<VariablesVO> items)
    {
        foreach (var item in items)
        {
            VariablesCache.Remove(item.Key);
        }

        return await Task.FromResult(true);
        //return await ShowSqlReturnMsg(result);
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnSaveAsync(VariablesVO item, ItemChangedType changedType)
    {
        VariablesCache.Set(item.Key, item.Value);
        return await Task.FromResult(true);
    }

}